{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## License \n",
    "\n",
    "Copyright 2017 - 2020 Patrick Hall and the H2O.ai team\n",
    "\n",
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**DISCLAIMER:** This notebook is not legal compliance advice."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Explain Your Predictive Models to Business Stakeholders using LIME with Python and H2O\n",
    "#### Describing complex models and generating reason codes with Local Interpretable Model-agnostic Explanations (LIME) and LIME-variants\n",
    "\n",
    "Local Interpretable Model-agnostic Explanations (LIME) shed light on how almost any machine learning model makes decisions for specific rows of data. LIME builds local linear surrogate models around observations of interest and leverages the highly interpretable properties of linear models to increase transparency and accountability for the corresponding model predictions. In this notebook, an h2o GBM is trained on the UCI credit card default data and then predictions for a highly risky customer are explained using linear model coefficients and LIME-derived reason codes. The notebook concludes by introducing a variant of LIME that is easier to execute on new data and that can be analyzed alongside observed (i.e., not simulated) data.\n",
    "\n",
    "**Note**: As of the h2o 3.24 \"Yates\" release, Shapley values are supported in h2o. Shapley values can be used in place of or along with LIME. To see Shapley values for an h2o GBM in action please see: https://github.com/jphall663/interpretable_machine_learning_with_python/blob/master/dia.ipynb."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Python imports\n",
    "In general, NumPy and Pandas will be used for data manipulation purposes and h2o will be used for modeling tasks. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# h2o Python API with specific classes\n",
    "import h2o \n",
    "from h2o.estimators.glm import H2OGeneralizedLinearEstimator # for LIME\n",
    "from h2o.grid.grid_search import H2OGridSearch               # for LIME\n",
    "from h2o.estimators.gbm import H2OGradientBoostingEstimator  # for GBM\n",
    "\n",
    "\n",
    "import operator # for sorting dictionaries\n",
    "\n",
    "import numpy as np   # array, vector, matrix calculations\n",
    "import pandas as pd  # DataFrame handling\n",
    "\n",
    "# display plots in notebook\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Start h2o\n",
    "H2o is both a library and a server. The machine learning algorithms in the library take advantage of the multithreaded and distributed architecture provided by the server to train machine learning algorithms extremely efficiently. The API for the library was imported above in cell 1, but the server still needs to be started."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Checking whether there is an H2O instance running at http://localhost:54321 ..... not found.\n",
      "Attempting to start a local H2O server...\n",
      "  Java Version: java version \"1.8.0_201\"; Java(TM) SE Runtime Environment (build 1.8.0_201-b09); Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)\n",
      "  Starting server from /home/patrickh/anaconda3/lib/python3.6/site-packages/h2o/backend/bin/h2o.jar\n",
      "  Ice root: /tmp/tmpq9cbjivf\n",
      "  JVM stdout: /tmp/tmpq9cbjivf/h2o_patrickh_started_from_python.out\n",
      "  JVM stderr: /tmp/tmpq9cbjivf/h2o_patrickh_started_from_python.err\n",
      "  Server is running at http://127.0.0.1:54321\n",
      "Connecting to H2O server at http://127.0.0.1:54321 ... successful.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div style=\"overflow:auto\"><table style=\"width:50%\"><tr><td>H2O cluster uptime:</td>\n",
       "<td>01 secs</td></tr>\n",
       "<tr><td>H2O cluster timezone:</td>\n",
       "<td>America/New_York</td></tr>\n",
       "<tr><td>H2O data parsing timezone:</td>\n",
       "<td>UTC</td></tr>\n",
       "<tr><td>H2O cluster version:</td>\n",
       "<td>3.26.0.3</td></tr>\n",
       "<tr><td>H2O cluster version age:</td>\n",
       "<td>4 days </td></tr>\n",
       "<tr><td>H2O cluster name:</td>\n",
       "<td>H2O_from_python_patrickh_zub5an</td></tr>\n",
       "<tr><td>H2O cluster total nodes:</td>\n",
       "<td>1</td></tr>\n",
       "<tr><td>H2O cluster free memory:</td>\n",
       "<td>1.778 Gb</td></tr>\n",
       "<tr><td>H2O cluster total cores:</td>\n",
       "<td>8</td></tr>\n",
       "<tr><td>H2O cluster allowed cores:</td>\n",
       "<td>8</td></tr>\n",
       "<tr><td>H2O cluster status:</td>\n",
       "<td>accepting new members, healthy</td></tr>\n",
       "<tr><td>H2O connection url:</td>\n",
       "<td>http://127.0.0.1:54321</td></tr>\n",
       "<tr><td>H2O connection proxy:</td>\n",
       "<td>None</td></tr>\n",
       "<tr><td>H2O internal security:</td>\n",
       "<td>False</td></tr>\n",
       "<tr><td>H2O API Extensions:</td>\n",
       "<td>Amazon S3, XGBoost, Algos, AutoML, Core V3, Core V4</td></tr>\n",
       "<tr><td>Python version:</td>\n",
       "<td>3.6.4 final</td></tr></table></div>"
      ],
      "text/plain": [
       "--------------------------  ---------------------------------------------------\n",
       "H2O cluster uptime:         01 secs\n",
       "H2O cluster timezone:       America/New_York\n",
       "H2O data parsing timezone:  UTC\n",
       "H2O cluster version:        3.26.0.3\n",
       "H2O cluster version age:    4 days\n",
       "H2O cluster name:           H2O_from_python_patrickh_zub5an\n",
       "H2O cluster total nodes:    1\n",
       "H2O cluster free memory:    1.778 Gb\n",
       "H2O cluster total cores:    8\n",
       "H2O cluster allowed cores:  8\n",
       "H2O cluster status:         accepting new members, healthy\n",
       "H2O connection url:         http://127.0.0.1:54321\n",
       "H2O connection proxy:\n",
       "H2O internal security:      False\n",
       "H2O API Extensions:         Amazon S3, XGBoost, Algos, AutoML, Core V3, Core V4\n",
       "Python version:             3.6.4 final\n",
       "--------------------------  ---------------------------------------------------"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "h2o.init(max_mem_size='2G')       # start h2o\n",
    "h2o.remove_all()                  # remove any existing data structures from h2o memory"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Download, explore, and prepare UCI credit card default data\n",
    "\n",
    "UCI credit card default data: https://archive.ics.uci.edu/ml/datasets/default+of+credit+card+clients\n",
    "\n",
    "The UCI credit card default data contains demographic and payment information about credit card customers in Taiwan in the year 2005. The data set contains 23 input variables: \n",
    "\n",
    "* **`LIMIT_BAL`**: Amount of given credit (NT dollar)\n",
    "* **`SEX`**: 1 = male; 2 = female\n",
    "* **`EDUCATION`**: 1 = graduate school; 2 = university; 3 = high school; 4 = others \n",
    "* **`MARRIAGE`**: 1 = married; 2 = single; 3 = others\n",
    "* **`AGE`**: Age in years \n",
    "* **`PAY_0`, `PAY_2` - `PAY_6`**: History of past payment; `PAY_0` = the repayment status in September, 2005; `PAY_2` = the repayment status in August, 2005; ...; `PAY_6` = the repayment status in April, 2005. The measurement scale for the repayment status is: -1 = pay duly; 1 = payment delay for one month; 2 = payment delay for two months; ...; 8 = payment delay for eight months; 9 = payment delay for nine months and above. \n",
    "* **`BILL_AMT1` - `BILL_AMT6`**: Amount of bill statement (NT dollar). `BILL_AMNT1` = amount of bill statement in September, 2005; `BILL_AMT2` = amount of bill statement in August, 2005; ...; `BILL_AMT6` = amount of bill statement in April, 2005. \n",
    "* **`PAY_AMT1` - `PAY_AMT6`**: Amount of previous payment (NT dollar). `PAY_AMT1` = amount paid in September, 2005; `PAY_AMT2` = amount paid in August, 2005; ...; `PAY_AMT6` = amount paid in April, 2005. \n",
    "\n",
    "These 23 input variables are used to predict the target variable, whether or not a customer defaulted on their credit card bill in late 2005.\n",
    "\n",
    "Because h2o accepts both numeric and character inputs, some variables will be recoded into more transparent character values."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Import data and clean\n",
    "The credit card default data is available as an `.xls` file. Pandas reads `.xls` files automatically, so it's used to load the credit card default data and give the prediction target a shorter name: `DEFAULT_NEXT_MONTH`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import XLS file\n",
    "path = 'default_of_credit_card_clients.xls'\n",
    "data = pd.read_excel(path,\n",
    "                     skiprows=1)\n",
    "\n",
    "# remove spaces from target column name \n",
    "data = data.rename(columns={'default payment next month': 'DEFAULT_NEXT_MONTH'}) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Assign modeling roles\n",
    "The shorthand name `y` is assigned to the prediction target. `X` is assigned to all other input variables in the credit card default data except the row indentifier, `ID`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "y = DEFAULT_NEXT_MONTH\n",
      "X = ['LIMIT_BAL', 'SEX', 'EDUCATION', 'MARRIAGE', 'AGE', 'PAY_0', 'PAY_2', 'PAY_3', 'PAY_4', 'PAY_5', 'PAY_6', 'BILL_AMT1', 'BILL_AMT2', 'BILL_AMT3', 'BILL_AMT4', 'BILL_AMT5', 'BILL_AMT6', 'PAY_AMT1', 'PAY_AMT2', 'PAY_AMT3', 'PAY_AMT4', 'PAY_AMT5', 'PAY_AMT6']\n"
     ]
    }
   ],
   "source": [
    "# assign target and inputs for GBM\n",
    "y = 'DEFAULT_NEXT_MONTH'\n",
    "X = [name for name in data.columns if name not in [y, 'ID']]\n",
    "print('y =', y)\n",
    "print('X =', X)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Helper function for recoding values in the UCI credict card default data\n",
    "This simple function maps longer, more understandable character string values from the UCI credit card default data dictionary to the original integer values of the input variables found in the dataset. These character values can be used directly in h2o decision tree models, and the function returns the original Pandas DataFrame as an h2o object, an H2OFrame. H2o models cannot run on Pandas DataFrames. They require H2OFrames."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Parse progress: |█████████████████████████████████████████████████████████| 100%\n"
     ]
    }
   ],
   "source": [
    "def recode_cc_data(frame):\n",
    "    \n",
    "    \"\"\" Recodes numeric categorical variables into categorical character variables\n",
    "    with more transparent values. \n",
    "    \n",
    "    Args:\n",
    "        frame: Pandas DataFrame version of UCI credit card default data.\n",
    "        \n",
    "    Returns: \n",
    "        H2OFrame with recoded values.\n",
    "        \n",
    "    \"\"\"\n",
    "    \n",
    "    # define recoded values\n",
    "    sex_dict = {1:'male', 2:'female'}\n",
    "    education_dict = {0:'other', 1:'graduate school', 2:'university', 3:'high school', \n",
    "                      4:'other', 5:'other', 6:'other'}\n",
    "    marriage_dict = {0:'other', 1:'married', 2:'single', 3:'divorced'}\n",
    "    pay_dict = {-2:'no consumption', -1:'pay duly', 0:'use of revolving credit', 1:'1 month delay', \n",
    "                2:'2 month delay', 3:'3 month delay', 4:'4 month delay', 5:'5 month delay', 6:'6 month delay', \n",
    "                7:'7 month delay', 8:'8 month delay', 9:'9+ month delay'}\n",
    "    \n",
    "    # recode values using Pandas apply() and anonymous function\n",
    "    frame['SEX'] = frame['SEX'].apply(lambda i: sex_dict[i])\n",
    "    frame['EDUCATION'] = frame['EDUCATION'].apply(lambda i: education_dict[i])    \n",
    "    frame['MARRIAGE'] = frame['MARRIAGE'].apply(lambda i: marriage_dict[i]) \n",
    "    for name in frame.columns:\n",
    "        if name in ['PAY_0', 'PAY_2', 'PAY_3', 'PAY_4', 'PAY_5', 'PAY_6']:\n",
    "            frame[name] = frame[name].apply(lambda i: pay_dict[i])            \n",
    "                \n",
    "    return h2o.H2OFrame(frame)\n",
    "\n",
    "data = recode_cc_data(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Ensure target is handled as a categorical variable\n",
    "In h2o, a numeric variable can be treated as numeric or categorical. The target variable `DEFAULT_NEXT_MONTH` takes on values of `0` or `1`. To ensure this numeric variable is treated as a categorical variable, the `asfactor()` function is used to explicitly declare that it is a categorical variable. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "data[y] = data[y].asfactor() "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Display descriptive statistics\n",
    "The h2o `describe()` function displays a brief description of the credit card default data. For the categorical input variables `LIMIT_BAL`, `SEX`, `EDUCATION`, `MARRIAGE`, and `PAY_0`-`PAY_6`, the new character values created above in cell 5 are visible. Basic descriptive statistics are displayed for numeric inputs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Rows:30000\n",
      "Cols:25\n",
      "\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<thead>\n",
       "<tr><th>       </th><th>ID               </th><th>LIMIT_BAL         </th><th>SEX   </th><th>EDUCATION      </th><th>MARRIAGE  </th><th>AGE              </th><th>PAY_0                  </th><th>PAY_2                  </th><th>PAY_3                  </th><th>PAY_4                  </th><th>PAY_5                  </th><th>PAY_6                  </th><th>BILL_AMT1        </th><th>BILL_AMT2        </th><th>BILL_AMT3        </th><th>BILL_AMT4        </th><th>BILL_AMT5        </th><th>BILL_AMT6        </th><th>PAY_AMT1          </th><th>PAY_AMT2          </th><th>PAY_AMT3          </th><th>PAY_AMT4          </th><th>PAY_AMT5          </th><th>PAY_AMT6          </th><th>DEFAULT_NEXT_MONTH  </th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr><td>type   </td><td>int              </td><td>int               </td><td>enum  </td><td>enum           </td><td>enum      </td><td>int              </td><td>enum                   </td><td>enum                   </td><td>enum                   </td><td>enum                   </td><td>enum                   </td><td>enum                   </td><td>int              </td><td>int              </td><td>int              </td><td>int              </td><td>int              </td><td>int              </td><td>int               </td><td>int               </td><td>int               </td><td>int               </td><td>int               </td><td>int               </td><td>enum                </td></tr>\n",
       "<tr><td>mins   </td><td>1.0              </td><td>10000.0           </td><td>      </td><td>               </td><td>          </td><td>21.0             </td><td>                       </td><td>                       </td><td>                       </td><td>                       </td><td>                       </td><td>                       </td><td>-165580.0        </td><td>-69777.0         </td><td>-157264.0        </td><td>-170000.0        </td><td>-81334.0         </td><td>-339603.0        </td><td>0.0               </td><td>0.0               </td><td>0.0               </td><td>0.0               </td><td>0.0               </td><td>0.0               </td><td>                    </td></tr>\n",
       "<tr><td>mean   </td><td>15000.5          </td><td>167484.32266666688</td><td>      </td><td>               </td><td>          </td><td>35.48549999999994</td><td>                       </td><td>                       </td><td>                       </td><td>                       </td><td>                       </td><td>                       </td><td>51223.33090000009</td><td>49179.07516666668</td><td>47013.15479999971</td><td>43262.9489666666 </td><td>40311.40096666653</td><td>38871.76039999991</td><td>5663.580500000014 </td><td>5921.16350000001  </td><td>5225.681500000005 </td><td>4826.076866666661 </td><td>4799.387633333302 </td><td>5215.502566666664 </td><td>                    </td></tr>\n",
       "<tr><td>maxs   </td><td>30000.0          </td><td>1000000.0         </td><td>      </td><td>               </td><td>          </td><td>79.0             </td><td>                       </td><td>                       </td><td>                       </td><td>                       </td><td>                       </td><td>                       </td><td>964511.0         </td><td>983931.0         </td><td>1664089.0        </td><td>891586.0         </td><td>927171.0         </td><td>961664.0         </td><td>873552.0          </td><td>1684259.0         </td><td>896040.0          </td><td>621000.0          </td><td>426529.0          </td><td>528666.0          </td><td>                    </td></tr>\n",
       "<tr><td>sigma  </td><td>8660.398374208891</td><td>129747.66156720225</td><td>      </td><td>               </td><td>          </td><td>9.21790406809016 </td><td>                       </td><td>                       </td><td>                       </td><td>                       </td><td>                       </td><td>                       </td><td>73635.86057552959</td><td>71173.76878252836</td><td>69349.38742703681</td><td>64332.85613391641</td><td>60797.1557702648 </td><td>59554.10753674574</td><td>16563.280354025763</td><td>23040.870402057226</td><td>17606.961469803115</td><td>15666.159744031993</td><td>15278.305679144793</td><td>17777.465775435332</td><td>                    </td></tr>\n",
       "<tr><td>zeros  </td><td>0                </td><td>0                 </td><td>      </td><td>               </td><td>          </td><td>0                </td><td>                       </td><td>                       </td><td>                       </td><td>                       </td><td>                       </td><td>                       </td><td>2008             </td><td>2506             </td><td>2870             </td><td>3195             </td><td>3506             </td><td>4020             </td><td>5249              </td><td>5396              </td><td>5968              </td><td>6408              </td><td>6703              </td><td>7173              </td><td>                    </td></tr>\n",
       "<tr><td>missing</td><td>0                </td><td>0                 </td><td>0     </td><td>0              </td><td>0         </td><td>0                </td><td>0                      </td><td>0                      </td><td>0                      </td><td>0                      </td><td>0                      </td><td>0                      </td><td>0                </td><td>0                </td><td>0                </td><td>0                </td><td>0                </td><td>0                </td><td>0                 </td><td>0                 </td><td>0                 </td><td>0                 </td><td>0                 </td><td>0                 </td><td>0                   </td></tr>\n",
       "<tr><td>0      </td><td>1.0              </td><td>20000.0           </td><td>female</td><td>university     </td><td>married   </td><td>24.0             </td><td>2 month delay          </td><td>2 month delay          </td><td>pay duly               </td><td>pay duly               </td><td>no consumption         </td><td>no consumption         </td><td>3913.0           </td><td>3102.0           </td><td>689.0            </td><td>0.0              </td><td>0.0              </td><td>0.0              </td><td>0.0               </td><td>689.0             </td><td>0.0               </td><td>0.0               </td><td>0.0               </td><td>0.0               </td><td>1                   </td></tr>\n",
       "<tr><td>1      </td><td>2.0              </td><td>120000.0          </td><td>female</td><td>university     </td><td>single    </td><td>26.0             </td><td>pay duly               </td><td>2 month delay          </td><td>use of revolving credit</td><td>use of revolving credit</td><td>use of revolving credit</td><td>2 month delay          </td><td>2682.0           </td><td>1725.0           </td><td>2682.0           </td><td>3272.0           </td><td>3455.0           </td><td>3261.0           </td><td>0.0               </td><td>1000.0            </td><td>1000.0            </td><td>1000.0            </td><td>0.0               </td><td>2000.0            </td><td>1                   </td></tr>\n",
       "<tr><td>2      </td><td>3.0              </td><td>90000.0           </td><td>female</td><td>university     </td><td>single    </td><td>34.0             </td><td>use of revolving credit</td><td>use of revolving credit</td><td>use of revolving credit</td><td>use of revolving credit</td><td>use of revolving credit</td><td>use of revolving credit</td><td>29239.0          </td><td>14027.0          </td><td>13559.0          </td><td>14331.0          </td><td>14948.0          </td><td>15549.0          </td><td>1518.0            </td><td>1500.0            </td><td>1000.0            </td><td>1000.0            </td><td>1000.0            </td><td>5000.0            </td><td>0                   </td></tr>\n",
       "<tr><td>3      </td><td>4.0              </td><td>50000.0           </td><td>female</td><td>university     </td><td>married   </td><td>37.0             </td><td>use of revolving credit</td><td>use of revolving credit</td><td>use of revolving credit</td><td>use of revolving credit</td><td>use of revolving credit</td><td>use of revolving credit</td><td>46990.0          </td><td>48233.0          </td><td>49291.0          </td><td>28314.0          </td><td>28959.0          </td><td>29547.0          </td><td>2000.0            </td><td>2019.0            </td><td>1200.0            </td><td>1100.0            </td><td>1069.0            </td><td>1000.0            </td><td>0                   </td></tr>\n",
       "<tr><td>4      </td><td>5.0              </td><td>50000.0           </td><td>male  </td><td>university     </td><td>married   </td><td>57.0             </td><td>pay duly               </td><td>use of revolving credit</td><td>pay duly               </td><td>use of revolving credit</td><td>use of revolving credit</td><td>use of revolving credit</td><td>8617.0           </td><td>5670.0           </td><td>35835.0          </td><td>20940.0          </td><td>19146.0          </td><td>19131.0          </td><td>2000.0            </td><td>36681.0           </td><td>10000.0           </td><td>9000.0            </td><td>689.0             </td><td>679.0             </td><td>0                   </td></tr>\n",
       "<tr><td>5      </td><td>6.0              </td><td>50000.0           </td><td>male  </td><td>graduate school</td><td>single    </td><td>37.0             </td><td>use of revolving credit</td><td>use of revolving credit</td><td>use of revolving credit</td><td>use of revolving credit</td><td>use of revolving credit</td><td>use of revolving credit</td><td>64400.0          </td><td>57069.0          </td><td>57608.0          </td><td>19394.0          </td><td>19619.0          </td><td>20024.0          </td><td>2500.0            </td><td>1815.0            </td><td>657.0             </td><td>1000.0            </td><td>1000.0            </td><td>800.0             </td><td>0                   </td></tr>\n",
       "<tr><td>6      </td><td>7.0              </td><td>500000.0          </td><td>male  </td><td>graduate school</td><td>single    </td><td>29.0             </td><td>use of revolving credit</td><td>use of revolving credit</td><td>use of revolving credit</td><td>use of revolving credit</td><td>use of revolving credit</td><td>use of revolving credit</td><td>367965.0         </td><td>412023.0         </td><td>445007.0         </td><td>542653.0         </td><td>483003.0         </td><td>473944.0         </td><td>55000.0           </td><td>40000.0           </td><td>38000.0           </td><td>20239.0           </td><td>13750.0           </td><td>13770.0           </td><td>0                   </td></tr>\n",
       "<tr><td>7      </td><td>8.0              </td><td>100000.0          </td><td>female</td><td>university     </td><td>single    </td><td>23.0             </td><td>use of revolving credit</td><td>pay duly               </td><td>pay duly               </td><td>use of revolving credit</td><td>use of revolving credit</td><td>pay duly               </td><td>11876.0          </td><td>380.0            </td><td>601.0            </td><td>221.0            </td><td>-159.0           </td><td>567.0            </td><td>380.0             </td><td>601.0             </td><td>0.0               </td><td>581.0             </td><td>1687.0            </td><td>1542.0            </td><td>0                   </td></tr>\n",
       "<tr><td>8      </td><td>9.0              </td><td>140000.0          </td><td>female</td><td>high school    </td><td>married   </td><td>28.0             </td><td>use of revolving credit</td><td>use of revolving credit</td><td>2 month delay          </td><td>use of revolving credit</td><td>use of revolving credit</td><td>use of revolving credit</td><td>11285.0          </td><td>14096.0          </td><td>12108.0          </td><td>12211.0          </td><td>11793.0          </td><td>3719.0           </td><td>3329.0            </td><td>0.0               </td><td>432.0             </td><td>1000.0            </td><td>1000.0            </td><td>1000.0            </td><td>0                   </td></tr>\n",
       "<tr><td>9      </td><td>10.0             </td><td>20000.0           </td><td>male  </td><td>high school    </td><td>single    </td><td>35.0             </td><td>no consumption         </td><td>no consumption         </td><td>no consumption         </td><td>no consumption         </td><td>pay duly               </td><td>pay duly               </td><td>0.0              </td><td>0.0              </td><td>0.0              </td><td>0.0              </td><td>13007.0          </td><td>13912.0          </td><td>0.0               </td><td>0.0               </td><td>0.0               </td><td>13007.0           </td><td>1122.0            </td><td>0.0               </td><td>0                   </td></tr>\n",
       "</tbody>\n",
       "</table>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "data.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Train an H2O GBM classifier"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Split data into training and test sets for early stopping\n",
    "The credit card default data is split into training and test sets to monitor and prevent overtraining. Reproducibility is also an important factor in creating trustworthy models, and randomly splitting datasets can introduce randomness in model predictions and other results. A random seed is used here to ensure the data split is reproducible."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train data rows = 21060, columns = 25\n",
      "Test data rows = 8940, columns = 25\n"
     ]
    }
   ],
   "source": [
    "# split into training and validation\n",
    "train, test = data.split_frame([0.7], seed=12345)\n",
    "\n",
    "# summarize split\n",
    "print('Train data rows = %d, columns = %d' % (train.shape[0], train.shape[1]))\n",
    "print('Test data rows = %d, columns = %d' % (test.shape[0], test.shape[1]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Train h2o GBM classifier\n",
    "Many tuning parameters must be specified to train a GBM using h2o. Typically a grid search would be performed to identify the best parameters for a given modeling task using the `H2OGridSearch` class. For brevity's sake, a previously-discovered set of good tuning parameters are specified here. Because gradient boosting methods typically resample training data, an additional random seed is also specified for the h2o GBM using the `seed` parameter to create reproducible predictions, error rates, and variable importance values. To avoid overfitting, the `stopping_rounds` parameter is used to stop the training process after the test error fails to decrease for 5 iterations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "gbm Model Build progress: |███████████████████████████████████████████████| 100%\n",
      "GBM Test AUC = 0.78\n"
     ]
    }
   ],
   "source": [
    "# initialize GBM model\n",
    "model = H2OGradientBoostingEstimator(ntrees=150,            # maximum 150 trees in GBM\n",
    "                                     max_depth=4,           # trees can have maximum depth of 4\n",
    "                                     sample_rate=0.9,       # use 90% of rows in each iteration (tree)\n",
    "                                     col_sample_rate=0.9,   # use 90% of variables in each iteration (tree)\n",
    "                                     stopping_rounds=5,     # stop if validation error does not decrease for 5 iterations (trees)\n",
    "                                     score_tree_interval=1, # for reproducibility, set higher for bigger data\n",
    "                                     seed=12345)            # random seed for reproducibility\n",
    "\n",
    "# train a GBM model\n",
    "model.train(y=y, x=X, training_frame=train, validation_frame=test)\n",
    "\n",
    "# print AUC\n",
    "print('GBM Test AUC = %.2f' % model.auc(valid=True))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Use LIME to generate descriptions for a local region with a perturbed sample\n",
    "\n",
    "LIME was originally described in the context of explaining image or text classification decisions here: http://www.kdd.org/kdd2016/papers/files/rfp0573-ribeiroA.pdf. It can certainly also be applied to business or customer data, as will be done in the remaining sections of this notebook. Multiple Python implementations of LIME are available from the original authors of LIME, from the eli5 package, from the skater package, and probably others. However, this notebook uses a simple, step-by-step implementation of LIME for instructional purposes. \n",
    "\n",
    "A linear model cannot be built on a single observation, so LIME typically requires that a set of rows similar to the row of interest be simulated. This set of records are scored using the complex model to be explained. Then the records are weighted by their closeness to the record of interest, and a regularized linear model is trained on this weighted explanatory set. The parameters of the linear model and LIME-derived reason codes are then used to explain the prediction for the selected record. Because simulation of new points can seem abstract to some practicioners and simulation and distance calculations can be somewhat burdensome for creating explanations quickly in mission-critical applications, this notebook also presents a variation of LIME in which a more practical sample, instead of a perturbed, simulated sample, is used to create a local region in which to fit a linear model."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Display the most risky customer\n",
    "In the Oriole notebook *Increase Transparency and Accountability in Your Machine Learning Project with Python and H2O*, row index 29116 was found to contain the riskiest customer in the test dataset according to the h2o GBM model. Sections 3-7 focus on deriving reason codes and other explanations for this customer's GBM prediction. The riskiest customer is selected first for analysis as an exercise in boundary testing."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<thead>\n",
       "<tr><th style=\"text-align: right;\">   ID</th><th style=\"text-align: right;\">  LIMIT_BAL</th><th>SEX   </th><th>EDUCATION  </th><th>MARRIAGE  </th><th style=\"text-align: right;\">  AGE</th><th>PAY_0        </th><th>PAY_2        </th><th>PAY_3        </th><th>PAY_4        </th><th>PAY_5        </th><th>PAY_6        </th><th style=\"text-align: right;\">  BILL_AMT1</th><th style=\"text-align: right;\">  BILL_AMT2</th><th style=\"text-align: right;\">  BILL_AMT3</th><th style=\"text-align: right;\">  BILL_AMT4</th><th style=\"text-align: right;\">  BILL_AMT5</th><th style=\"text-align: right;\">  BILL_AMT6</th><th style=\"text-align: right;\">  PAY_AMT1</th><th style=\"text-align: right;\">  PAY_AMT2</th><th style=\"text-align: right;\">  PAY_AMT3</th><th style=\"text-align: right;\">  PAY_AMT4</th><th style=\"text-align: right;\">  PAY_AMT5</th><th style=\"text-align: right;\">  PAY_AMT6</th><th style=\"text-align: right;\">  DEFAULT_NEXT_MONTH</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr><td style=\"text-align: right;\">29116</td><td style=\"text-align: right;\">      20000</td><td>female</td><td>university </td><td>married   </td><td style=\"text-align: right;\">   59</td><td>3 month delay</td><td>2 month delay</td><td>3 month delay</td><td>2 month delay</td><td>2 month delay</td><td>4 month delay</td><td style=\"text-align: right;\">       8803</td><td style=\"text-align: right;\">      11137</td><td style=\"text-align: right;\">      10672</td><td style=\"text-align: right;\">      11201</td><td style=\"text-align: right;\">      12721</td><td style=\"text-align: right;\">      11946</td><td style=\"text-align: right;\">      2800</td><td style=\"text-align: right;\">         0</td><td style=\"text-align: right;\">      1000</td><td style=\"text-align: right;\">      2000</td><td style=\"text-align: right;\">         0</td><td style=\"text-align: right;\">         0</td><td style=\"text-align: right;\">                   1</td></tr>\n",
       "</tbody>\n",
       "</table>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "row = test[test['ID'] == 29116]\n",
    "row"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To use LIME, a sample of similar (i.e., near or local) points is simulated around the customer of interest. This simple function draws numeric values from normal distributions centered around the customer of interest and draws categorical values at random from the variable values in the test set."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>LIMIT_BAL</th>\n",
       "      <th>SEX</th>\n",
       "      <th>EDUCATION</th>\n",
       "      <th>MARRIAGE</th>\n",
       "      <th>AGE</th>\n",
       "      <th>PAY_0</th>\n",
       "      <th>PAY_2</th>\n",
       "      <th>PAY_3</th>\n",
       "      <th>PAY_4</th>\n",
       "      <th>PAY_5</th>\n",
       "      <th>...</th>\n",
       "      <th>BILL_AMT3</th>\n",
       "      <th>BILL_AMT4</th>\n",
       "      <th>BILL_AMT5</th>\n",
       "      <th>BILL_AMT6</th>\n",
       "      <th>PAY_AMT1</th>\n",
       "      <th>PAY_AMT2</th>\n",
       "      <th>PAY_AMT3</th>\n",
       "      <th>PAY_AMT4</th>\n",
       "      <th>PAY_AMT5</th>\n",
       "      <th>PAY_AMT6</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>9988.454213</td>\n",
       "      <td>female</td>\n",
       "      <td>graduate school</td>\n",
       "      <td>divorced</td>\n",
       "      <td>58.287510</td>\n",
       "      <td>5 month delay</td>\n",
       "      <td>5 month delay</td>\n",
       "      <td>5 month delay</td>\n",
       "      <td>5 month delay</td>\n",
       "      <td>6 month delay</td>\n",
       "      <td>...</td>\n",
       "      <td>5433.340804</td>\n",
       "      <td>6276.576876</td>\n",
       "      <td>8055.530587</td>\n",
       "      <td>7347.467911</td>\n",
       "      <td>1597.834490</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1000.000000</td>\n",
       "      <td>823.253257</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>181039.642122</td>\n",
       "      <td>male</td>\n",
       "      <td>high school</td>\n",
       "      <td>married</td>\n",
       "      <td>70.460689</td>\n",
       "      <td>pay duly</td>\n",
       "      <td>pay duly</td>\n",
       "      <td>pay duly</td>\n",
       "      <td>pay duly</td>\n",
       "      <td>use of revolving credit</td>\n",
       "      <td>...</td>\n",
       "      <td>94937.888614</td>\n",
       "      <td>90412.278099</td>\n",
       "      <td>87766.906051</td>\n",
       "      <td>85915.192926</td>\n",
       "      <td>22137.303918</td>\n",
       "      <td>25583.930273</td>\n",
       "      <td>21802.010398</td>\n",
       "      <td>20928.433066</td>\n",
       "      <td>19123.775929</td>\n",
       "      <td>22563.515833</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>20000.000000</td>\n",
       "      <td>male</td>\n",
       "      <td>university</td>\n",
       "      <td>single</td>\n",
       "      <td>43.284233</td>\n",
       "      <td>7 month delay</td>\n",
       "      <td>7 month delay</td>\n",
       "      <td>7 month delay</td>\n",
       "      <td>7 month delay</td>\n",
       "      <td>8 month delay</td>\n",
       "      <td>...</td>\n",
       "      <td>10672.000000</td>\n",
       "      <td>11201.000000</td>\n",
       "      <td>12721.000000</td>\n",
       "      <td>11946.000000</td>\n",
       "      <td>2800.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1000.000000</td>\n",
       "      <td>2000.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3 rows × 23 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       LIMIT_BAL     SEX        EDUCATION  MARRIAGE        AGE          PAY_0  \\\n",
       "0    9988.454213  female  graduate school  divorced  58.287510  5 month delay   \n",
       "1  181039.642122    male      high school   married  70.460689       pay duly   \n",
       "2   20000.000000    male       university    single  43.284233  7 month delay   \n",
       "\n",
       "           PAY_2          PAY_3          PAY_4                    PAY_5  \\\n",
       "0  5 month delay  5 month delay  5 month delay            6 month delay   \n",
       "1       pay duly       pay duly       pay duly  use of revolving credit   \n",
       "2  7 month delay  7 month delay  7 month delay            8 month delay   \n",
       "\n",
       "       ...          BILL_AMT3     BILL_AMT4     BILL_AMT5     BILL_AMT6  \\\n",
       "0      ...        5433.340804   6276.576876   8055.530587   7347.467911   \n",
       "1      ...       94937.888614  90412.278099  87766.906051  85915.192926   \n",
       "2      ...       10672.000000  11201.000000  12721.000000  11946.000000   \n",
       "\n",
       "       PAY_AMT1      PAY_AMT2      PAY_AMT3      PAY_AMT4      PAY_AMT5  \\\n",
       "0   1597.834490      0.000000   1000.000000    823.253257      0.000000   \n",
       "1  22137.303918  25583.930273  21802.010398  20928.433066  19123.775929   \n",
       "2   2800.000000      0.000000   1000.000000   2000.000000      0.000000   \n",
       "\n",
       "       PAY_AMT6  \n",
       "0      0.000000  \n",
       "1  22563.515833  \n",
       "2      0.000000  \n",
       "\n",
       "[3 rows x 23 columns]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def generate_local_sample(row, frame, X, N=1000):\n",
    "    \n",
    "    \"\"\" Generates a perturbed sample around a row of interest.\n",
    "    \n",
    "    Args:\n",
    "        row: Row of H2OFrame to be explained.\n",
    "        frame: H2OFrame in which row is stored.\n",
    "        X: List of model input variables.\n",
    "        N: Number of samples to generate.\n",
    "    \n",
    "    Returns:\n",
    "        Pandas DataFrame containing perturbed sample.\n",
    "    \n",
    "    \"\"\"\n",
    "    \n",
    "    # initialize Pandas DataFrame\n",
    "    sample_frame = pd.DataFrame(data=np.zeros(shape=(N, len(X))), columns=X)\n",
    "    \n",
    "    # generate column vectors of \n",
    "    # randomly drawn levels for categorical variables\n",
    "    # normally distributed numeric values around mean of column for numeric variables\n",
    "    for key, val in frame[X].types.items():\n",
    "        if val == 'enum': # 'enum' means categorical\n",
    "            rs = np.random.RandomState(11111) # random seed for reproducibility\n",
    "            draw = rs.choice(frame[key].levels()[0], size=(1, N))[0]\n",
    "        else:\n",
    "            rs = np.random.RandomState(11111) # random seed for reproducibility\n",
    "            loc = row[key][0, 0]\n",
    "            sd = frame[key].sd()\n",
    "            draw = rs.normal(loc, sd, (N, 1))\n",
    "            draw[draw < 0] = loc # prevents unrealistic values when std. dev. is large\n",
    "        \n",
    "        sample_frame[key] = draw\n",
    "        \n",
    "    return sample_frame\n",
    "\n",
    "# run and display results\n",
    "perturbed_sample = generate_local_sample(row, test, X)\n",
    "perturbed_sample.head(n=3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Calculate distance between row of interest and perturbed sample\n",
    "Once the sample is simulated, then distances from the point of interest are used to weigh each point before fitting a penalized regression model. Since Euclidean distance calculations require numeric quanitites, categorical input variables are one-hot encoded. (Pandas has convenient functionality for one-hot encoding, and the H2OFrames are temporarily casted back to Pandas DataFrames to perform the encoding.) To prevent the disparate scales of numeric values, such as `AGE` and `LIMIT_BAL`, from  skewing Euclidean distances, numeric input variables are standardized. \n",
    "\n",
    "First, the row containing the riskiest customer is encoded and standardized."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Parse progress: |█████████████████████████████████████████████████████████| 100%\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<thead>\n",
       "<tr><th style=\"text-align: right;\">  LIMIT_BAL</th><th style=\"text-align: right;\">     AGE</th><th style=\"text-align: right;\">  BILL_AMT1</th><th style=\"text-align: right;\">  BILL_AMT2</th><th style=\"text-align: right;\">  BILL_AMT3</th><th style=\"text-align: right;\">  BILL_AMT4</th><th style=\"text-align: right;\">  BILL_AMT5</th><th style=\"text-align: right;\">  BILL_AMT6</th><th style=\"text-align: right;\">  PAY_AMT1</th><th style=\"text-align: right;\">  PAY_AMT2</th><th style=\"text-align: right;\">  PAY_AMT3</th><th style=\"text-align: right;\">  PAY_AMT4</th><th style=\"text-align: right;\">  PAY_AMT5</th><th style=\"text-align: right;\">  PAY_AMT6</th><th style=\"text-align: right;\">  SEX_female</th><th style=\"text-align: right;\">  EDUCATION_graduate school</th><th style=\"text-align: right;\">  MARRIAGE_married</th><th style=\"text-align: right;\">  PAY_0_3 month delay</th><th style=\"text-align: right;\">  PAY_2_2 month delay</th><th style=\"text-align: right;\">  PAY_3_2 month delay</th><th style=\"text-align: right;\">  PAY_4_3 month delay</th><th style=\"text-align: right;\">  PAY_5_3 month delay</th><th style=\"text-align: right;\">  PAY_6_3 month delay</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr><td style=\"text-align: right;\">    2.24639</td><td style=\"text-align: right;\">0.481433</td><td style=\"text-align: right;\">   -0.66112</td><td style=\"text-align: right;\">  -0.657958</td><td style=\"text-align: right;\">  -0.651883</td><td style=\"text-align: right;\">  -0.637776</td><td style=\"text-align: right;\">  -0.622867</td><td style=\"text-align: right;\">  -0.609179</td><td style=\"text-align: right;\"> -0.360791</td><td style=\"text-align: right;\"> -0.282325</td><td style=\"text-align: right;\"> -0.315203</td><td style=\"text-align: right;\"> -0.319038</td><td style=\"text-align: right;\"> -0.319074</td><td style=\"text-align: right;\"> -0.270536</td><td style=\"text-align: right;\">           1</td><td style=\"text-align: right;\">                          1</td><td style=\"text-align: right;\">                 1</td><td style=\"text-align: right;\">                    1</td><td style=\"text-align: right;\">                    1</td><td style=\"text-align: right;\">                    1</td><td style=\"text-align: right;\">                    1</td><td style=\"text-align: right;\">                    1</td><td style=\"text-align: right;\">                    1</td></tr>\n",
       "</tbody>\n",
       "</table>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# scaling and one-hot encoding for calculating Euclidian distance\n",
    "# for the row of interest\n",
    "\n",
    "# scale numeric\n",
    "numeric = list(set(X) - set(['ID', 'SEX', 'EDUCATION', 'MARRIAGE', 'PAY_0', 'PAY_2',\n",
    "                             'PAY_3', 'PAY_4', 'PAY_5', 'PAY_6', 'DEFAULT_NEXT_MONTH']))\n",
    "\n",
    "scaled_test = test.as_data_frame()\n",
    "scaled_test[numeric] = (scaled_test[numeric] - scaled_test[numeric].mean())/scaled_test[numeric].std()\n",
    "    \n",
    "# encode categorical\n",
    "row_df = scaled_test[scaled_test['ID'] == 22760]\n",
    "row_dummies = pd.concat([row_df.drop(['ID', 'SEX', 'EDUCATION', 'MARRIAGE', 'PAY_0', 'PAY_2',\n",
    "                                      'PAY_3', 'PAY_4', 'PAY_5', 'PAY_6', 'DEFAULT_NEXT_MONTH'], axis=1),\n",
    "                        pd.get_dummies(row_df[['SEX', 'EDUCATION', 'MARRIAGE', 'PAY_0',\n",
    "                                               'PAY_2', 'PAY_3', 'PAY_4', 'PAY_5', 'PAY_6']])], \n",
    "                        axis=1)\n",
    "\n",
    "# convert to H2OFrame\n",
    "row_dummies = h2o.H2OFrame(row_dummies)\n",
    "row_dummies"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then the simulated sample is encoded and standardized."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Parse progress: |█████████████████████████████████████████████████████████| 100%\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<thead>\n",
       "<tr><th style=\"text-align: right;\">  LIMIT_BAL</th><th style=\"text-align: right;\">       AGE</th><th style=\"text-align: right;\">  BILL_AMT1</th><th style=\"text-align: right;\">  BILL_AMT2</th><th style=\"text-align: right;\">  BILL_AMT3</th><th style=\"text-align: right;\">  BILL_AMT4</th><th style=\"text-align: right;\">  BILL_AMT5</th><th style=\"text-align: right;\">  BILL_AMT6</th><th style=\"text-align: right;\">  PAY_AMT1</th><th style=\"text-align: right;\">  PAY_AMT2</th><th style=\"text-align: right;\">  PAY_AMT3</th><th style=\"text-align: right;\">  PAY_AMT4</th><th style=\"text-align: right;\">  PAY_AMT5</th><th style=\"text-align: right;\">  PAY_AMT6</th><th style=\"text-align: right;\">  SEX_female</th><th style=\"text-align: right;\">  EDUCATION_graduate school</th><th style=\"text-align: right;\">  MARRIAGE_married</th><th style=\"text-align: right;\">  PAY_0_3 month delay</th><th style=\"text-align: right;\">  PAY_2_2 month delay</th><th style=\"text-align: right;\">  PAY_3_2 month delay</th><th style=\"text-align: right;\">  PAY_4_3 month delay</th><th style=\"text-align: right;\">  PAY_5_3 month delay</th><th style=\"text-align: right;\">  PAY_6_3 month delay</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr><td style=\"text-align: right;\">  -0.845634</td><td style=\"text-align: right;\">-0.0955699</td><td style=\"text-align: right;\">  -0.84979 </td><td style=\"text-align: right;\">  -0.845174</td><td style=\"text-align: right;\">  -0.845174</td><td style=\"text-align: right;\">  -0.83886 </td><td style=\"text-align: right;\">  -0.834367</td><td style=\"text-align: right;\">  -0.834967</td><td style=\"text-align: right;\"> -0.83834 </td><td style=\"text-align: right;\"> -0.723503</td><td style=\"text-align: right;\"> -0.722209</td><td style=\"text-align: right;\"> -0.849042</td><td style=\"text-align: right;\"> -0.723503</td><td style=\"text-align: right;\"> -0.723503</td><td style=\"text-align: right;\">           1</td><td style=\"text-align: right;\">                          1</td><td style=\"text-align: right;\">                 0</td><td style=\"text-align: right;\">                    0</td><td style=\"text-align: right;\">                    0</td><td style=\"text-align: right;\">                    0</td><td style=\"text-align: right;\">                    0</td><td style=\"text-align: right;\">                    0</td><td style=\"text-align: right;\">                    0</td></tr>\n",
       "<tr><td style=\"text-align: right;\">   1.41903 </td><td style=\"text-align: right;\"> 1.2011   </td><td style=\"text-align: right;\">   1.41949 </td><td style=\"text-align: right;\">   1.41897 </td><td style=\"text-align: right;\">   1.41897 </td><td style=\"text-align: right;\">   1.4182  </td><td style=\"text-align: right;\">   1.4176  </td><td style=\"text-align: right;\">   1.41769 </td><td style=\"text-align: right;\">  1.41814 </td><td style=\"text-align: right;\">  1.41999 </td><td style=\"text-align: right;\">  1.41993 </td><td style=\"text-align: right;\">  1.41941 </td><td style=\"text-align: right;\">  1.41999 </td><td style=\"text-align: right;\">  1.41999 </td><td style=\"text-align: right;\">           0</td><td style=\"text-align: right;\">                          0</td><td style=\"text-align: right;\">                 1</td><td style=\"text-align: right;\">                    0</td><td style=\"text-align: right;\">                    0</td><td style=\"text-align: right;\">                    0</td><td style=\"text-align: right;\">                    0</td><td style=\"text-align: right;\">                    0</td><td style=\"text-align: right;\">                    0</td></tr>\n",
       "<tr><td style=\"text-align: right;\">  -0.713084</td><td style=\"text-align: right;\">-1.69369  </td><td style=\"text-align: right;\">  -0.716971</td><td style=\"text-align: right;\">  -0.712654</td><td style=\"text-align: right;\">  -0.712654</td><td style=\"text-align: right;\">  -0.706755</td><td style=\"text-align: right;\">  -0.70256 </td><td style=\"text-align: right;\">  -0.70312 </td><td style=\"text-align: right;\"> -0.706269</td><td style=\"text-align: right;\"> -0.723503</td><td style=\"text-align: right;\"> -0.722209</td><td style=\"text-align: right;\"> -0.716271</td><td style=\"text-align: right;\"> -0.723503</td><td style=\"text-align: right;\"> -0.723503</td><td style=\"text-align: right;\">           0</td><td style=\"text-align: right;\">                          0</td><td style=\"text-align: right;\">                 0</td><td style=\"text-align: right;\">                    0</td><td style=\"text-align: right;\">                    0</td><td style=\"text-align: right;\">                    0</td><td style=\"text-align: right;\">                    0</td><td style=\"text-align: right;\">                    0</td><td style=\"text-align: right;\">                    0</td></tr>\n",
       "</tbody>\n",
       "</table>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# scaling and one-hot encoding for calculating Euclidian distance \n",
    "# for the simulated sample\n",
    "\n",
    "# scale\n",
    "scaled_perturbed_sample = perturbed_sample[numeric].copy(deep=True)\n",
    "scaled_perturbed_sample = (scaled_perturbed_sample - scaled_perturbed_sample.mean())/scaled_perturbed_sample.std()\n",
    "\n",
    "# encode\n",
    "perturbed_sample_dummies = pd.concat([scaled_perturbed_sample,\n",
    "                                      pd.get_dummies(perturbed_sample[['SEX', 'EDUCATION', 'MARRIAGE', 'PAY_0',\n",
    "                                                                       'PAY_2', 'PAY_3', 'PAY_4', 'PAY_5', 'PAY_6']])],\n",
    "                                     axis=1)\n",
    "\n",
    "# convert to H2OFrame\n",
    "perturbed_sample_dummies = h2o.H2OFrame(perturbed_sample_dummies[row_dummies.columns])\n",
    "perturbed_sample_dummies.head(rows=3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Distance is calculated using h2o. The distance is substracted from the maximum distance, changing the distance values into similarity values. Now the observations with the highest values are those that are closest to the observation of interest and they will carry the most weight in the local explanatory linear model. A few sample similarity values are displayed directly below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<thead>\n",
       "<tr><th style=\"text-align: right;\">  distance</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr><td style=\"text-align: right;\">   13.2143</td></tr>\n",
       "<tr><td style=\"text-align: right;\">   10.2477</td></tr>\n",
       "<tr><td style=\"text-align: right;\">   12.6506</td></tr>\n",
       "</tbody>\n",
       "</table>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# calculate distance using H2OFrame distance function\n",
    "distance = row_dummies.distance(perturbed_sample_dummies, measure='l2').transpose()\n",
    "distance.columns = ['distance']          # rename \n",
    "distance = distance.max() - distance     # lower distances, higher weight in LIME\n",
    "distance.head(rows=3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Bind distance weights onto perturbed sample\n",
    "To fit an h2o linear model using the similarities as observation weights, the distance column must reside in the same H2OFrame as the simulated sample data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Parse progress: |█████████████████████████████████████████████████████████| 100%\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<thead>\n",
       "<tr><th style=\"text-align: right;\">  LIMIT_BAL</th><th>SEX   </th><th>EDUCATION      </th><th>MARRIAGE  </th><th style=\"text-align: right;\">    AGE</th><th>PAY_0        </th><th>PAY_2        </th><th>PAY_3        </th><th>PAY_4        </th><th>PAY_5                  </th><th>PAY_6                  </th><th style=\"text-align: right;\">  BILL_AMT1</th><th style=\"text-align: right;\">  BILL_AMT2</th><th style=\"text-align: right;\">  BILL_AMT3</th><th style=\"text-align: right;\">  BILL_AMT4</th><th style=\"text-align: right;\">  BILL_AMT5</th><th style=\"text-align: right;\">  BILL_AMT6</th><th style=\"text-align: right;\">  PAY_AMT1</th><th style=\"text-align: right;\">  PAY_AMT2</th><th style=\"text-align: right;\">  PAY_AMT3</th><th style=\"text-align: right;\">  PAY_AMT4</th><th style=\"text-align: right;\">  PAY_AMT5</th><th style=\"text-align: right;\">  PAY_AMT6</th><th style=\"text-align: right;\">  distance</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr><td style=\"text-align: right;\">    9988.45</td><td>female</td><td>graduate school</td><td>divorced  </td><td style=\"text-align: right;\">58.2875</td><td>5 month delay</td><td>5 month delay</td><td>5 month delay</td><td>5 month delay</td><td>6 month delay          </td><td>6 month delay          </td><td style=\"text-align: right;\">    3152.62</td><td style=\"text-align: right;\">    5678.04</td><td style=\"text-align: right;\">    5433.34</td><td style=\"text-align: right;\">    6276.58</td><td style=\"text-align: right;\">    8055.53</td><td style=\"text-align: right;\">    7347.47</td><td style=\"text-align: right;\">   1597.83</td><td style=\"text-align: right;\">       0  </td><td style=\"text-align: right;\">      1000</td><td style=\"text-align: right;\">   823.253</td><td style=\"text-align: right;\">       0  </td><td style=\"text-align: right;\">       0  </td><td style=\"text-align: right;\">   13.2143</td></tr>\n",
       "<tr><td style=\"text-align: right;\">  181040   </td><td>male  </td><td>high school    </td><td>married   </td><td style=\"text-align: right;\">70.4607</td><td>pay duly     </td><td>pay duly     </td><td>pay duly     </td><td>pay duly     </td><td>use of revolving credit</td><td>use of revolving credit</td><td style=\"text-align: right;\">   99691.6 </td><td style=\"text-align: right;\">   98946.5 </td><td style=\"text-align: right;\">   94937.9 </td><td style=\"text-align: right;\">   90412.3 </td><td style=\"text-align: right;\">   87766.9 </td><td style=\"text-align: right;\">   85915.2 </td><td style=\"text-align: right;\">  22137.3 </td><td style=\"text-align: right;\">   25583.9</td><td style=\"text-align: right;\">     21802</td><td style=\"text-align: right;\"> 20928.4  </td><td style=\"text-align: right;\">   19123.8</td><td style=\"text-align: right;\">   22563.5</td><td style=\"text-align: right;\">   10.2477</td></tr>\n",
       "<tr><td style=\"text-align: right;\">   20000   </td><td>male  </td><td>university     </td><td>single    </td><td style=\"text-align: right;\">43.2842</td><td>7 month delay</td><td>7 month delay</td><td>7 month delay</td><td>7 month delay</td><td>8 month delay          </td><td>8 month delay          </td><td style=\"text-align: right;\">    8803   </td><td style=\"text-align: right;\">   11137   </td><td style=\"text-align: right;\">   10672   </td><td style=\"text-align: right;\">   11201   </td><td style=\"text-align: right;\">   12721   </td><td style=\"text-align: right;\">   11946   </td><td style=\"text-align: right;\">   2800   </td><td style=\"text-align: right;\">       0  </td><td style=\"text-align: right;\">      1000</td><td style=\"text-align: right;\">  2000    </td><td style=\"text-align: right;\">       0  </td><td style=\"text-align: right;\">       0  </td><td style=\"text-align: right;\">   12.6506</td></tr>\n",
       "</tbody>\n",
       "</table>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "perturbed_sample = h2o.H2OFrame(perturbed_sample).cbind(distance)\n",
    "perturbed_sample.head(rows=3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Bind model predictions onto perturbed sample\n",
    "For LIME, the target of the explanatory local linear model is the predictions of the GBM model in the local simulated sample. The values are calculated and column-bound to the simulated sample. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "gbm prediction progress: |████████████████████████████████████████████████| 100%\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<thead>\n",
       "<tr><th style=\"text-align: right;\">  LIMIT_BAL</th><th>SEX   </th><th>EDUCATION      </th><th>MARRIAGE  </th><th style=\"text-align: right;\">    AGE</th><th>PAY_0        </th><th>PAY_2        </th><th>PAY_3        </th><th>PAY_4        </th><th>PAY_5                  </th><th>PAY_6                  </th><th style=\"text-align: right;\">  BILL_AMT1</th><th style=\"text-align: right;\">  BILL_AMT2</th><th style=\"text-align: right;\">  BILL_AMT3</th><th style=\"text-align: right;\">  BILL_AMT4</th><th style=\"text-align: right;\">  BILL_AMT5</th><th style=\"text-align: right;\">  BILL_AMT6</th><th style=\"text-align: right;\">  PAY_AMT1</th><th style=\"text-align: right;\">  PAY_AMT2</th><th style=\"text-align: right;\">  PAY_AMT3</th><th style=\"text-align: right;\">  PAY_AMT4</th><th style=\"text-align: right;\">  PAY_AMT5</th><th style=\"text-align: right;\">  PAY_AMT6</th><th style=\"text-align: right;\">  distance</th><th style=\"text-align: right;\">  p_DEFAULT_NEXT_MONTH</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr><td style=\"text-align: right;\">    9988.45</td><td>female</td><td>graduate school</td><td>divorced  </td><td style=\"text-align: right;\">58.2875</td><td>5 month delay</td><td>5 month delay</td><td>5 month delay</td><td>5 month delay</td><td>6 month delay          </td><td>6 month delay          </td><td style=\"text-align: right;\">    3152.62</td><td style=\"text-align: right;\">    5678.04</td><td style=\"text-align: right;\">    5433.34</td><td style=\"text-align: right;\">    6276.58</td><td style=\"text-align: right;\">    8055.53</td><td style=\"text-align: right;\">    7347.47</td><td style=\"text-align: right;\">   1597.83</td><td style=\"text-align: right;\">       0  </td><td style=\"text-align: right;\">      1000</td><td style=\"text-align: right;\">   823.253</td><td style=\"text-align: right;\">       0  </td><td style=\"text-align: right;\">       0  </td><td style=\"text-align: right;\">   13.2143</td><td style=\"text-align: right;\">             0.515197 </td></tr>\n",
       "<tr><td style=\"text-align: right;\">  181040   </td><td>male  </td><td>high school    </td><td>married   </td><td style=\"text-align: right;\">70.4607</td><td>pay duly     </td><td>pay duly     </td><td>pay duly     </td><td>pay duly     </td><td>use of revolving credit</td><td>use of revolving credit</td><td style=\"text-align: right;\">   99691.6 </td><td style=\"text-align: right;\">   98946.5 </td><td style=\"text-align: right;\">   94937.9 </td><td style=\"text-align: right;\">   90412.3 </td><td style=\"text-align: right;\">   87766.9 </td><td style=\"text-align: right;\">   85915.2 </td><td style=\"text-align: right;\">  22137.3 </td><td style=\"text-align: right;\">   25583.9</td><td style=\"text-align: right;\">     21802</td><td style=\"text-align: right;\"> 20928.4  </td><td style=\"text-align: right;\">   19123.8</td><td style=\"text-align: right;\">   22563.5</td><td style=\"text-align: right;\">   10.2477</td><td style=\"text-align: right;\">             0.0663977</td></tr>\n",
       "<tr><td style=\"text-align: right;\">   20000   </td><td>male  </td><td>university     </td><td>single    </td><td style=\"text-align: right;\">43.2842</td><td>7 month delay</td><td>7 month delay</td><td>7 month delay</td><td>7 month delay</td><td>8 month delay          </td><td>8 month delay          </td><td style=\"text-align: right;\">    8803   </td><td style=\"text-align: right;\">   11137   </td><td style=\"text-align: right;\">   10672   </td><td style=\"text-align: right;\">   11201   </td><td style=\"text-align: right;\">   12721   </td><td style=\"text-align: right;\">   11946   </td><td style=\"text-align: right;\">   2800   </td><td style=\"text-align: right;\">       0  </td><td style=\"text-align: right;\">      1000</td><td style=\"text-align: right;\">  2000    </td><td style=\"text-align: right;\">       0  </td><td style=\"text-align: right;\">       0  </td><td style=\"text-align: right;\">   12.6506</td><td style=\"text-align: right;\">             0.692681 </td></tr>\n",
       "</tbody>\n",
       "</table>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "yhat = 'p_DEFAULT_NEXT_MONTH'\n",
    "preds1 = model.predict(perturbed_sample).drop(['predict', 'p0'])\n",
    "preds1.columns = [yhat]\n",
    "perturbed_sample = perturbed_sample.cbind(preds1)\n",
    "perturbed_sample.head(rows=3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Train penalized linear model in local region \n",
    "Once the simulated sample has been weighted with distances and contains the GBM model predictions, a linear model is fit to the original inputs and the GBM model predictions, weighted by similarity to the row of interest. The trained GLM coefficients are helpful for understanding the local region of response function around the riskiest customer."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "glm Model Build progress: |███████████████████████████████████████████████| 100%\n",
      "\n",
      "Local Positive GLM Coefficients:\n",
      "MARRIAGE.married:         0.005757170307944079\n",
      "EDUCATION.high school:    0.005757170307945231\n",
      "PAY_0.8 month delay:      0.0065111741471265\n",
      "PAY_4.8 month delay:      0.006511174147127027\n",
      "PAY_3.8 month delay:      0.006511174147127041\n",
      "PAY_2.8 month delay:      0.006511174147127305\n",
      "PAY_0.4 month delay:      0.010754792563841487\n",
      "PAY_2.4 month delay:      0.010754792563842083\n",
      "PAY_4.4 month delay:      0.010754792563842167\n",
      "PAY_3.4 month delay:      0.010754792563842194\n",
      "PAY_5.6 month delay:      0.013679631302936743\n",
      "PAY_6.6 month delay:      0.01367963130293677\n",
      "PAY_2.2 month delay:      0.02636889214079144\n",
      "PAY_0.2 month delay:      0.02636889214079166\n",
      "PAY_3.2 month delay:      0.02636889214079169\n",
      "PAY_4.2 month delay:      0.026368892140792216\n",
      "PAY_0.3 month delay:      0.030938016884379638\n",
      "PAY_2.3 month delay:      0.03093801688437975\n",
      "PAY_4.3 month delay:      0.030938016884379915\n",
      "PAY_3.3 month delay:      0.030938016884380054\n",
      "PAY_2.7 month delay:      0.03649976537701373\n",
      "PAY_3.7 month delay:      0.036499765377013785\n",
      "PAY_0.7 month delay:      0.03649976537701445\n",
      "PAY_4.7 month delay:      0.036499765377014534\n",
      "PAY_6.3 month delay:      0.04135072490974577\n",
      "PAY_5.3 month delay:      0.0413507249097461\n",
      "Intercept:                0.5259242681066165\n",
      "\n",
      "Local GLM R-square:\n",
      "0.84\n"
     ]
    }
   ],
   "source": [
    "# initialize\n",
    "local_glm1 = H2OGeneralizedLinearEstimator(weights_column='distance',\n",
    "                                           seed=12345)\n",
    "# train \n",
    "local_glm1.train(x=X, y=yhat, training_frame=perturbed_sample)\n",
    "\n",
    "# coefs\n",
    "print('\\nLocal Positive GLM Coefficients:')\n",
    "for c_name, c_val in sorted(local_glm1.coef().items(), key=operator.itemgetter(1)):\n",
    "    if c_val > 0.0:\n",
    "        print('%s %s' % (str(c_name + ':').ljust(25), c_val))\n",
    "        \n",
    "# r2\n",
    "print('\\nLocal GLM R-square:\\n%.2f' % local_glm1.r2())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The coefficients of the local linear model describe the average behavior of the GBM response function around the riskiest customer. In this local region, customers who missed payments are treated as the most likely to default."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Generate reason codes with LIME based on a perturbed sample\n",
    "This basic function uses the coefficients of the local linear explanatory model and the values in the row of interest to plot reason code values in a bar chart. The local GLM coefficient multiplied by the value in a specific row are estimates of how much each variable contributed to each prediction decision. These values can tell you how a variable and its values were weighted in any given decision by the model. These values are crucially important for machine learning interpretability and are often to referred to \"local feature importance\", \"reason codes\", or \"turn-down codes.\" The latter phrases are borrowed from credit scoring. Credit lenders in the U.S. must provide reasons for turning down certain credit applications in an automated fashion. Reason codes can be easily extracted from LIME local feature importance values by simply ranking the variables that played the largest role in any given decision."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plot_local_contrib(row, model, X, g_pred=None, scale=False): \n",
    "\n",
    "    \"\"\" Plots reason codes in a bar chart. \n",
    "    \n",
    "    Args:\n",
    "    \n",
    "        row: Row of H2OFrame to be explained.\n",
    "        model: H2O linear model used for generating reason codes.\n",
    "        X: List of model input variables.\n",
    "        g_pred: Prediction of model to be explained, sometimes denoted g, used for scaling.\n",
    "        scale: Whether to rescale contributions to sum to model predictions.\n",
    "    \n",
    "    \"\"\"\n",
    "    \n",
    "    # initialize Pandas DataFrame to store results\n",
    "    local_contrib_frame = pd.DataFrame(columns=['Name', 'Local Contribution', 'Sign'])\n",
    "    \n",
    "    # multiply values in row by local glm coefficients    \n",
    "    for key, val in sorted(row[X].types.items()):\n",
    "        contrib = 0\n",
    "        name = ''\n",
    "        if val == 'enum':\n",
    "                level = row[key][0, 0]\n",
    "                name = '.'.join([str(key), str(level)])\n",
    "                if name in model.coef():\n",
    "                    contrib = model.coef()[name]\n",
    "        else:\n",
    "            name = key\n",
    "            if name in model.coef():\n",
    "                contrib = row[name][0, 0]*model.coef()[name]\n",
    "        \n",
    "        # save only non-zero values\n",
    "        if contrib != 0.0:\n",
    "            local_contrib_frame = local_contrib_frame.append({'Name': name,\n",
    "                                                              'Local Contribution': contrib,\n",
    "                                                              'Sign': contrib > 0}, \n",
    "                                                             ignore_index=True) \n",
    "\n",
    "    if scale:\n",
    "        scaler = (g_pred - model.coef()['Intercept']) /\\\n",
    "            local_contrib_frame['Local Contribution'].sum()\n",
    "        local_contrib_frame['Local Contribution']  *= scaler\n",
    "        \n",
    "    # plot\n",
    "    _ = local_contrib_frame.plot(x='Name',\n",
    "                                 y='Local Contribution',\n",
    "                                 kind='bar', \n",
    "                                 title='Reason Codes', \n",
    "                                 color=local_contrib_frame.Sign.map({True:'b', False:'g'}), \n",
    "                                 legend=False) \n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Display reason codes\n",
    "Here it can be seen that the riskiest customer's prediction is driven by her values for payment variables. Specifically, the top five LIME-derived reason codes contributing to her high probability of default are:\n",
    "\n",
    "1. Most recent payment is 3 months delayed.\n",
    "2. 3rd most recent payment is 3 months delayed.\n",
    "3. 2nd most recent payment is 2 months delayed.\n",
    "4. 4th most recent payment is 2 months delayed.\n",
    "5. Customer is married.\n",
    "\n",
    "(Of course variables like `MARRIAGE`, `AGE`, and `SEX`should not be used in credit lending decisions. For a slightly more careful treatment of GBM in the context of fair lending see: https://github.com/jphall663/interpretable_machine_learning_with_python/blob/master/dia.ipynb)\n",
    "\n",
    "This result is somewhat aligned with LOCO-derived reason codes found in section 5 of the *Increase Transparency and Accountability in Your Machine Learning Project with Python and H2O* Oriole notebook. Both perspectives weigh the riskiest customer's most recent and 3rd most recent payments very heavily in the model's prediction. A minor discrepancy between LOCO- and LIME-derived reason codes is somewhat expected. LIME explanations are linear, do not consider interactions, and represent offsets from the local linear model intercept. LOCO importance values are nonlinear, do consider interactions, and do not explicitly consider a linear intercept or offset. **Because most currently-available explanatory techniques are approximate, it is recommended that users employ several different explanatory techniques and trust only consisent results across techniques. Also, as of h2o 3.24, Shapley values are supported for h2o GBM. Use Shapley values along with or instead of LIME for any high-stakes application.**\n",
    "\n",
    "It is also imperative to compare these results to domain knowledge and reasonable expectations. In this case, the LIME reason codes and linear model coefficients tell a relatively parsimonious story about the GBM's prediction behavior. If this was not so, steps should be taken to either reconcile or remove inconsistencies and unreasonable predictions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAF2CAYAAABu5+/BAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAIABJREFUeJzt3XmcXXV9//HXm4RNlmBDEE2AIEEtiIqOCEqrwg+EKoSyCLiASsWqVOrWgq0blipu1AWxWECgKigWjUVFEcSVZQJoQMWGAEJAZRMQFIi8f3+c75CbcbYkc7/3ZO77+XjcR+4958w973tg7mfOOd9FtomIiFir1wEiIqIdUhAiIgJIQYiIiCIFISIigBSEiIgoUhAiIgJIQYhYo0j6rqS/63WOmJpSEKLVJN0o6Q+Sfi/p15I+K2nDXudaGZJ2kvR1Sb+TdJekyyW9ute5IoZLQYg1wT62NwSeAewIHNvjPBMmaRfgIuASYB4wE3g9sHcvc0WMJAUh1hi2fw1cQFMYAJC0rqQPS/qVpN9I+rSk9cu6x0r6X0m3S7q7PJ/T8bOvkrRE0n2SbpD08rJ8LUn/KukmSb+VdKakGWXdXEmWdHjZ5x2S/mWM2B8CzrB9gu073Fho+6UdOV4raXE5e1gg6Qkd6/aQ9AtJ90j6JKDON5f0Gkk/L5/vAklbleWSdGLJf6+kRZKeuhqHP/pACkKsMcqX+d7A4o7FHwCeRFMk5gGzgXeVdWsBpwNbAVs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REQGkIERERJGCEBERQNP7co2x6aabeu7cub2OERGxRlm4cOEdtmeNt90aVRDmzp3L4OBgr2NERKxRJN00/la5ZBQREUUKQkREACkIERFRpCBERASQghAREUUKQkREACkIERFRrFH9ECLWVNLqv8dUGak+x6K9coYQERFACkJERBQTKgiS9pJ0naTFko4ZYf26ks4p6y+TNLcs30nS1eXxE0l/O9H3jIiIusYtCJKmASfRzNW6HXCopO2GbXYEcLftecCJNPO8QjPH64DtZwB7Af8pafoE3zMiIiqayBnCTsBi20tsPwScDcwfts184Izy/Fxgd0my/YDtZWX5esDQraCJvGdERFQ0kYIwG7i54/UtZdmI25QCcA8wE0DScyRdCywC/r6sn8h7Un7+SEmDkgZvv/32CcSNiIhV0fWbyrYvs7098GzgWEnrreTPn2J7wPbArFnjDucdERGraCIFYSmwRcfrOWXZiNtImg7MAO7s3MD2z4HfA0+d4HtGRERFEykIVwDbStpa0jrAIcCCYdssAA4vzw8ELrLt8jPTASRtBTwFuHGC7xkRERWN21PZ9jJJRwEXANOA02xfK+k4YND2AuBU4CxJi4G7aL7gAXYFjpH0MPAI8AbbdwCM9J6T/NkiImIlyGtQH/CBgQFnCs1YE2W4huVyLOqTtND2wHjbpadyREQAKQgREVGkIEREBJCCEBERRQpCREQAKQgREVGkIEREBJCCEBERRQpCREQAKQgREVGkIEREBJCCEBERRQpCREQAKQgREVGkIEREBDCBCXIiYurIXATL5Vj8uZwhREQEkIIQERFFCkJERAApCBERUUyoIEjaS9J1khZLOmaE9etKOqesv0zS3LJ8D0kLJS0q/+7W8TPfLe95dXlsNlkfKiIiVt64rYwkTQNOAvYAbgGukLTA9s86NjsCuNv2PEmHACcABwN3APvYvlXSU4ELgNkdP/dy24OT9FkiImI1TOQMYSdgse0lth8CzgbmD9tmPnBGeX4usLsk2b7K9q1l+bXA+pLWnYzgERExuSZSEGYDN3e8voUV/8pfYRvby4B7gJnDtjkAuNL2gx3LTi+Xi94pTUar4IiIWFVVbipL2p7mMtLrOha/3PYOwF+VxytH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oTNIjwPeBI4aGnpa0xPYT+y1HGzK0KUevpWNaRH37A7cBF0v6jKTd6U3z1zbkaEOGNuXoqZwhRPSIpA2A+TSXKXajmdP4vNotXdqQow0Z2pSjV1IQIlqgzKB2EHCw7d37OUcbMrQpR00pCBGVlfmC/x6YBywCTh0+hWS/5GhDhjbl6LUUhIjKJJ1DM1Pb94G9gZtsH92POdqQoU05ei0FIaIySYts71CeTwcut/3MfszRhgxtytFraWUUUd/DQ096fFmiDTnakKFNOXoqZwgRlUn6E03nOGiaNq4PPEDFznFtydGGDG3K0WspCBERAeSSUUREFCkIEREBpCBERESRghDRQZIlfaTj9dskvaeHkSKqSUGIWNGDwP6SNu11kIjaUhAiVrQMOAV48/AVkvaRdJmkqyRdKOlxZfl7JJ0h6fuSbpK0v6QPSlok6ZuS1i7bPUvSJZIWSrpA0uPrfrSIsaUgRPy5k4CXS5oxbPkPgJ1t7wicDfxTx7ptaEbH3Bf4b+Di0vP1D8CLS1H4BHCg7WcBpwHHd/djRKyczJgWMYzte8tUm2+i+UIfMgc4p/xlvw7N9IpDvmH7YUmLgGnAN8vyRTTzNj+ZZurOb5dZ2qbRjL8f0Ro5Q4gY2X8ARwAbdCz7BPDJ8pf/64D1OtY9CGD7EeBhL+/x+QjNH14CrrX9jPLYwfae3f4QESsjBSFiBLbvAr5IUxSGzACWlueHr+RbXgfMkrQLgKS1JW2/2kEjJlEKQsToPgJ0tjZ6D/AlSQtpJmKfMNsPAQcCJ0j6CXA18NxJyhkxKTKWUUREADlDiIiIIgUhIiKAFISIiChSECIiAkhBiIiIIgUhIiKAFISIiChSECIiAoD/D0RWJqkQOaYSAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fe5cb77a860>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_local_contrib(row, local_glm1, X)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. Use LIME to generate descriptions for a local region with a practical sample\n",
    "Using a previously-existing local sample based on clusters, deciles, or other more natural segments to create LIME explanations is computationally cheaper and perhaps more straightforward than using a simulated perturbed sample, but it does have one major drawback. If the sample is too large, the explanatory linear model maybe not be accurate enough to explain all predictions in the sample. The remaining sections of this notebook will explore the idea of generating LIME explanations using a practical sample. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Create a local region based on values of SEX and merge with GBM model predictions\n",
    "Instead of using a perturbed simulated sample, a linear model will be fit on all women in the test set, and the sample is not weighted by distance from any one point. A few lines of the all female sample are displayed directly below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "gbm prediction progress: |████████████████████████████████████████████████| 100%\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<thead>\n",
       "<tr><th style=\"text-align: right;\">  ID</th><th style=\"text-align: right;\">  LIMIT_BAL</th><th>SEX   </th><th>EDUCATION  </th><th>MARRIAGE  </th><th style=\"text-align: right;\">  AGE</th><th>PAY_0                  </th><th>PAY_2                  </th><th>PAY_3                  </th><th>PAY_4                  </th><th>PAY_5                  </th><th>PAY_6                  </th><th style=\"text-align: right;\">  BILL_AMT1</th><th style=\"text-align: right;\">  BILL_AMT2</th><th style=\"text-align: right;\">  BILL_AMT3</th><th style=\"text-align: right;\">  BILL_AMT4</th><th style=\"text-align: right;\">  BILL_AMT5</th><th style=\"text-align: right;\">  BILL_AMT6</th><th style=\"text-align: right;\">  PAY_AMT1</th><th style=\"text-align: right;\">  PAY_AMT2</th><th style=\"text-align: right;\">  PAY_AMT3</th><th style=\"text-align: right;\">  PAY_AMT4</th><th style=\"text-align: right;\">  PAY_AMT5</th><th style=\"text-align: right;\">  PAY_AMT6</th><th style=\"text-align: right;\">  DEFAULT_NEXT_MONTH</th><th style=\"text-align: right;\">  p_DEFAULT_NEXT_MONTH</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr><td style=\"text-align: right;\">   4</td><td style=\"text-align: right;\">      50000</td><td>female</td><td>university </td><td>married   </td><td style=\"text-align: right;\">   37</td><td>use of revolving credit</td><td>use of revolving credit</td><td>use of revolving credit</td><td>use of revolving credit</td><td>use of revolving credit</td><td>use of revolving credit</td><td style=\"text-align: right;\">      46990</td><td style=\"text-align: right;\">      48233</td><td style=\"text-align: right;\">      49291</td><td style=\"text-align: right;\">      28314</td><td style=\"text-align: right;\">      28959</td><td style=\"text-align: right;\">      29547</td><td style=\"text-align: right;\">      2000</td><td style=\"text-align: right;\">      2019</td><td style=\"text-align: right;\">      1200</td><td style=\"text-align: right;\">      1100</td><td style=\"text-align: right;\">      1069</td><td style=\"text-align: right;\">      1000</td><td style=\"text-align: right;\">                   0</td><td style=\"text-align: right;\">              0.144991</td></tr>\n",
       "<tr><td style=\"text-align: right;\">   8</td><td style=\"text-align: right;\">     100000</td><td>female</td><td>university </td><td>single    </td><td style=\"text-align: right;\">   23</td><td>use of revolving credit</td><td>pay duly               </td><td>pay duly               </td><td>use of revolving credit</td><td>use of revolving credit</td><td>pay duly               </td><td style=\"text-align: right;\">      11876</td><td style=\"text-align: right;\">        380</td><td style=\"text-align: right;\">        601</td><td style=\"text-align: right;\">        221</td><td style=\"text-align: right;\">       -159</td><td style=\"text-align: right;\">        567</td><td style=\"text-align: right;\">       380</td><td style=\"text-align: right;\">       601</td><td style=\"text-align: right;\">         0</td><td style=\"text-align: right;\">       581</td><td style=\"text-align: right;\">      1687</td><td style=\"text-align: right;\">      1542</td><td style=\"text-align: right;\">                   0</td><td style=\"text-align: right;\">              0.128193</td></tr>\n",
       "<tr><td style=\"text-align: right;\">  16</td><td style=\"text-align: right;\">      50000</td><td>female</td><td>high school</td><td>divorced  </td><td style=\"text-align: right;\">   23</td><td>1 month delay          </td><td>2 month delay          </td><td>use of revolving credit</td><td>use of revolving credit</td><td>use of revolving credit</td><td>use of revolving credit</td><td style=\"text-align: right;\">      50614</td><td style=\"text-align: right;\">      29173</td><td style=\"text-align: right;\">      28116</td><td style=\"text-align: right;\">      28771</td><td style=\"text-align: right;\">      29531</td><td style=\"text-align: right;\">      30211</td><td style=\"text-align: right;\">         0</td><td style=\"text-align: right;\">      1500</td><td style=\"text-align: right;\">      1100</td><td style=\"text-align: right;\">      1200</td><td style=\"text-align: right;\">      1300</td><td style=\"text-align: right;\">      1100</td><td style=\"text-align: right;\">                   0</td><td style=\"text-align: right;\">              0.325205</td></tr>\n",
       "</tbody>\n",
       "</table>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "preds2 = model.predict(test).drop(['predict', 'p0'])\n",
    "preds2.columns = [yhat]\n",
    "practical_sample = test.cbind(preds2)\n",
    "practical_sample = practical_sample[practical_sample['SEX'] == 'female']\n",
    "practical_sample.head(rows=3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Train penalized linear model in local region \n",
    "A penalized linear model is trained in the local region defined by women in the test set. Because fit is a concern in this much larger explanatory sample, users should always check the R<sup>2</sup> or other goodness-of-fit measures to ensure surrogate model is accurate in the sample. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "glm Model Build progress: |███████████████████████████████████████████████| 100%\n",
      "\n",
      "Local Positive GLM Coefficients:\n",
      "BILL_AMT5:                1.0399411952809277e-07\n",
      "BILL_AMT2:                1.1999977329358182e-07\n",
      "AGE:                      0.0005425600656944679\n",
      "EDUCATION.high school:    0.0007216688611868718\n",
      "PAY_0.8 month delay:      0.004556460822481314\n",
      "PAY_2.7 month delay:      0.004556460822481664\n",
      "PAY_3.6 month delay:      0.004556460822481671\n",
      "EDUCATION.university:     0.004929360519089897\n",
      "PAY_3.3 month delay:      0.009929773165479217\n",
      "MARRIAGE.married:         0.010735128951809985\n",
      "PAY_6.2 month delay:      0.018087589078182496\n",
      "PAY_5.3 month delay:      0.022758693814452228\n",
      "PAY_2.2 month delay:      0.026627792771502998\n",
      "PAY_4.2 month delay:      0.028646065263953614\n",
      "PAY_2.3 month delay:      0.028779499175080204\n",
      "PAY_5.2 month delay:      0.041320673312054176\n",
      "PAY_5.7 month delay:      0.04168881231553721\n",
      "PAY_4.7 month delay:      0.04168881231553731\n",
      "PAY_6.3 month delay:      0.11704990177038722\n",
      "PAY_0.4 month delay:      0.14562584980767246\n",
      "PAY_0.3 month delay:      0.21475212459213328\n",
      "PAY_0.2 month delay:      0.2253878590232612\n",
      "Intercept:                0.4675657228203593\n",
      "\n",
      "Local GLM R-square:\n",
      "0.93\n"
     ]
    }
   ],
   "source": [
    "# initialize\n",
    "local_glm2 = H2OGeneralizedLinearEstimator(seed=12345)\n",
    "\n",
    "# train \n",
    "local_glm2.train(x=X, y=yhat, training_frame=practical_sample)\n",
    "\n",
    "# coefs\n",
    "print('\\nLocal Positive GLM Coefficients:')\n",
    "for c_name, c_val in sorted(local_glm2.coef().items(), key=operator.itemgetter(1)):\n",
    "    if c_val > 0.0:\n",
    "        print('%s %s' % (str(c_name + ':').ljust(25), c_val))\n",
    "        \n",
    "# r2\n",
    "print('\\nLocal GLM R-square:\\n%.2f' % local_glm2.r2())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The R<sup>2</sup> is quite high for this local sample and linear model. Because the sample is simply the women in the test set and because the model fit is acceptable, the trained linear model and coefficients can be used to understand the average behavior of women in the test set. On average, late payments, particulary `PAY_0`, `PAY_2`, and `PAY_6`, are the most likely to push the GBM model towards higher probability of default values for women."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6. Generate a ranked predictions plot to assess validity of local explanatory model\n",
    "A *ranked predictions plot* can also be used to ensure the local linear surrogate model is a good fit for the model inputs and predictions. A ranked predictions plot is a way to visually check whether the surrogate model is a good fit for the complex model. The y-axis is the numeric prediction of both models for a given point. The x-axis is the rank of a point when the predictions are sorted by their GBM prediction, from lowest on the left to highest on the right. When both sets of predictions are aligned, as they are below, this a good indication that the linear model fits the complex, nonlinear GBM well in the practical sample."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "glm prediction progress: |████████████████████████████████████████████████| 100%\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX8AAAEICAYAAAC3Y/QeAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAIABJREFUeJzs3Xd4VFX6wPHvO2mEJCSkQIAgCb03KTaagiAo9i6KWFZde+8glnVdVtd19aeuBXdtWFZFRUERBAGR3jsECDWFQEIISWbO748pmUxmkkky6e/neXjI3HvuPWcG8s65p4oxBqWUUo2LpbYLoJRSquZp8FdKqUZIg79SSjVCGvyVUqoR0uCvlFKNkAZ/pZRqhDT4q1ojIlNE5MMA3StVREYG4l5VyVtEHheRdyp5nw0iMjyghaskERkuImm1XQ5VfTT4qzI5AtsJEckVkYMiMl1EImu7XBXhKHOB4z1kichPItK1OvIyxrxgjLnZzzI953FtD2PM/Oool48yTBQRq+NzOSYiq0Xk/Ercp9R7UXWfBn/ljwuMMZFAX6Af8Fgtl6cyXnK8hyTgMDDdWyIRCa7JQtUBSxyfSwzwLvCZiDSv5TKpGqDBX/nNGHMQmI39SwAAERknIqscNce9IjLF7VyyiBgRuUFE9ohIhog84e3eIhIiIp+IyJciEioiFhF5VER2iEimiHwmIrFu6SeIyG7HOa/39PEe8oCPgZ6O+0wRkS9E5EMROQZMrErenk1ZInKWiCwWkWzH5zNRRG4FrgUedtS6v3WkdW8+ChORf4jIfseff4hImOPccBFJE5EHROSwi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tB7dm/djMkX9PB5/txzz2Xq1Kl07tyZkSNHcuWVVzJs2LBy77tx40Z+++03wsPDmT59OitXrmTt2rXExsby5Zdfsnr1atasWUNGRgYDBw5k6NChLFq0iNTUVDZu3Mjhw4fp1q0bkyZNAuDOO+/k6aftuyhOmDCB7777jssuu4x//etfTJs2jQEDBlBYWMhdd93FN998Q0JCAjNmzOCJJ57gvffe81nOpUuXYrFYSEiwrwmVlpbG4sWLCQoK4vHHH+fss8/mvffeIzs7m0GDBjFy5EjeeustmjZtyqZNm1i7di39+/cHYPXq1ezbt4/169cDkJ2d7d8/glKqRtTb4F8bIiMjWbFiBQsXLmTevHlceeWVvPjii0ycOLHM68aPH094eLjr9ahRo4iNjQXgt99+4+qrryYoKIiWLVsybNgwli1bxm+//cbll1+OxWIhMTGRESNGuK6fN28eL730Enl5eWRlZdGjRw8uuOCCEnlu2bKF9evXM2rUKMBeE2/VqpXX8r3yyit8+OGHREVFMWPGDNfIm8svv5ygoCAA5syZw8yZM5k2bRpgH3a7Z88eFixYwN133w1A79696d27NwDt27dn586d3HXXXYwbN45zzz3Xr89YKVUz6m3wL6uGXp2CgoIYPnw4w4cPp1evXnzwwQdMnDiR4OBgbDYbQKnx6hEREWW+roj8/HzuuOMOli9fTtu2bZkyZYrX8fHGGHr06MGSJUvKvaezzd+TezmNMXz55Zd06dLFr3I2b96cNWvWMHv2bN58800+++yzMp86lGrsVu45wiVvLK6x/LTNvwK2bNnCtm3bXK9Xr15Nu3btAHub/4oVKwD48ssv/b7nkCFDmDFjBlarlfT0dBYsWMCgQYM488wz+fLLL7HZbBw6dIj58+cDxV8s8fHx5ObmuvoBAKKiosjJyQGgS5cupKenu4J/YWEhGzZsqPR7Hz16NK+99hrGGABWrVoFwNChQ/n4448BWL9+PWvXrgUgIyMDm83GpZdeynPPPcfKlSsrnbdSjYEz8E8Jns60kDerPb96W/OvDbm5udx1111kZ2cTHBxMx44defvttwGYPHkyN910E0899ZSrs9cfF198MUuWLKFPnz6ICC+99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Onq5py0TfRZ/xIugRYSnqusRNY6d0G66voXgnssPTEsBNY5W/LzABmAbv/HxXFmNlaM5tqZtNJf1M7zOwuMtEn6VJJYyo2aVx1k8n4LKTsJ85DXUhP4o+S1lw7yo7nX8T9GtAH/E5aK7yftE76AdADbAfGeV8Bz7vGQ0Bb1XXuIz1I6wXuLVuXx7SAtKZ6ENjvZUVG+q4F9rm+buAx988kJbde4A1gpPtHeb3X22dWXavDdR8Bbi5bWw2ti/jnbZ8s9LmOA14+reSNXMZnUYntHYIgCFqQ3JZ9giAIggsgkn8QBEELEsk/CIKgBYnkHwRB0IJE8g+CIGhBIvkHQRC0IJH8gyAIWpC/AUQjl60KodC9AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fe5af3d2550>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# ranked predictions plot\n",
    "pred_frame = local_glm2.predict(practical_sample).cbind(practical_sample)\\\n",
    "                       .as_data_frame()[['predict', yhat]]\n",
    "\n",
    "pred_frame.columns = ['Surrogate Preds.', 'ML Preds.']\n",
    "pred_frame.sort_values(by='ML Preds.', inplace=True)\n",
    "pred_frame.reset_index(inplace=True, drop=True)\n",
    "_ = pred_frame.plot(title='Ranked Predictions Plot')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Both the R<sup>2</sup> and ranked predictions plot show the linear model is a good fit in the practical, approximately local sample. This means the regression coefficients are likely a very accurate representation of the behavior of the nonlinear model in this region."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 7. Generate reason codes using a practical sample"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Create explanations (or 'reason codes') for a row in the local set\n",
    "Reason codes are generated for the model based on the practical sample, just as they were for the model based on the perturbed sample. Again, the woman's value for `PAY_0` is the most important local contributor to her GBM prediction. As seen in other attempts to explain this prediction, `PAY_0` followed by other payment variables, marital status, and her age play a role in the model decision. Also, for better local accuracy and explainability, LIME contributions are scaled such that contributions for each prediction plus the LIME intercept term sum to the GBM's predictions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "gbm prediction progress: |████████████████████████████████████████████████| 100%\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX0AAAF8CAYAAAAjJWv0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAIABJREFUeJzt3XmYXGWZ/vHvTYCwhwBRNOyLIIgKtgiCG8imIzgIEhQFh9+gjigjKoOigjguoLigiKKAgKOgCBpFRURAHdk67OsQg2wihk0QEAh5fn+8b5PqSi91Tp3qOp1zf66rrlSdU/XUczrdT516z7soIjAzs2ZYqt8JmJnZxHHRNzNrEBd9M7MGcdE3M2sQF30zswZx0TczaxAXfbMaknSxpP/X7zxsyeOib7Ug6c+SnpD0D0l/lfRdSSv1O68iJG0t6ReSHpb0oKQrJL2r33mZtXLRtzp5U0SsBLwU2BL4aJ/z6ZikbYHfApcAGwGrA+8FdutnXmbtXPStdiLir8D5pOIPgKSpkr4o6U5J90n6pqTl877pkn4uab6kh/L9tVpee4CkeZIelXS7pLfn7UtJ+rikOyT9TdLpkqblfetJCkn75/e8X9IRY6T9BeC0iDgmIu6PZE5EvLUlj3+XNDd/C5gt6fkt+3aSdIukv0v6OqDW4JL+TdLN+fjOl7Ru3i5JX875PyLpekkv6uLHb0s4F32rnVywdwPmtmz+PPAC0gfBRsBM4JN531LAqcC6wDrAE8DXc6wVgeOB3SJiZeCVwDX5dQfk2+uADYCVhl7XYntgE2BH4JOSXjhCvisA2wJnj3FMOwCfA94KPA+4Azgz71sDOAf4OLAG8Cdgu5bX7g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56JuZNYibd8zMGsRF38ysQVz0rZEkhaTjWh5/WNJRfUzJbEK46FtTPQnsKWmNfidiNpFc9K2pFgAnAR9s3yHpTZIul3S1pN9Iem7efpSk0yT9XtIdkvaUdKyk6yX9StIy+Xkvk3SJpDmSzpf0vIk9NLPRuehbk50AvD2v4NXqD8A2EbElcCZwWMu+DUmLcu8OfA+4KC/w8QTwxlz4vwbsFREvA04BPtPbwzDrnKdWtsaKiEcknQ58gFS0h6wFnJXP0JcFbm/Z98uIeFrS9cAU4Fd5+/XAesAmwIuAC/JiH1OAe3t5HGZF+Ezfmu4rwIHAii3bvgZ8PZ/BvxtYrmXfk5BW+AKejkUDXRaSTqIE3BgRL823LSJi514fhFmnXPSt0SLiQeCHpMI/ZBpwT76/f8GQtwIzJG0LIGkZSZt3nahZRVz0zeA4oLUXz1HAjyTNAe4vEigingL2Ao6RdC1wDfDKivI065qnYTAzaxCf6ZuZNYiLvplZg7jom5k1iIu+mVmDuOibmTWIi76ZWYO46JuZNcj/B4KzglXGT4QlAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7fe5aeeacf28>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "g_pred_ = model.predict(row)['p1'][0, 0]\n",
    "plot_local_contrib(row, local_glm2, X, g_pred=g_pred_, scale=True)                                                             "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Shutdown H2O\n",
    "After using h2o, it's typically best to shut it down. However, before doing so, users should ensure that they have saved any h2o data structures, such as models and H2OFrames, or scoring artifacts, such as POJOs and MOJOs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Are you sure you want to shutdown the H2O instance running at http://127.0.0.1:54321 (Y/N)? y\n",
      "H2O session _sid_898f closed.\n"
     ]
    }
   ],
   "source": [
    "# be careful, this can erase your work!\n",
    "h2o.cluster().shutdown(prompt=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Summary\n",
    "In this notebook, LIME was used to explain and generate reason codes for a complex GBM classifier. To do so, local linear models were fit to appropriate, representative samples and linear model coefficients were used to explain the average behavior in the samples and to create reason codes. Reason codes were assesed against domain knowledge and reasonable expectations. A ranked prediction plot was also introduced to compare surrogate linear model predictions to GBM model predictions. These techniques should generalize well for many types of business and research problems, enabling you to train a complex machine learning model and analyze, validate, and explain it to your colleagues, bosses, and potentially, external regulators."
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
